<template>
  <div class="register-item">
    <!-- <button @click="isShow = !isShow">按钮</button> -->
    <!-- appear -->
    <!-- enter-active-class="animated animate__wobble" -->
    <!-- leave-active-class="animated animate__wobble" -->
    <transition name="bounce">
      <el-card class="box-card" v-if="isShow">
        <el-form
          class="form"
          :model="loginForm"
          :rules="loginFormRules"
          ref="loginFormRef"
        >
          <!-- 账号 -->
          <el-form-item class="username" prop="username" v-if="isShow">
            <span class="span1">用户名:</span>
            <el-input
              class="username1"
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入内容"
              prefix-icon="el-icon-user-solid"
            ></el-input>
          </el-form-item>

          <!-- 密码 -->
          <el-form-item class="password" prop="password" v-if="isShow">
            <span>密码:</span>
            <el-input
              class="password1"
              placeholder="请输入密码"
              v-model="loginForm.password"
              show-password
              prefix-icon="el-icon-lock"
              ref="password"
            ></el-input>
          </el-form-item>

          <!-- 按钮 -->
          <el-form-item class="btns" v-if="isShow">
            <el-button class="el_button" type="primary" @click="login"
              >登录</el-button
            >
            <el-button class="el_button" type="info" @click="resetLoginForm"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </el-card>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      usernameJudge: false,
      passwordJudge: false,
      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法 都是element ui里面的规则
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          } /* 鼠标失焦触发验证 */,
          {
            min: 3,
            max: 10,
            message: "长度在 3 ~ 10 个字符",
            trigger: "blur",
          },
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 ~ 15 个字符",
            trigger: "blur",
          },
        ],
      },
      isShow: true,
    };
  },
  methods: {
    //   登录验证
    login() {
      // /*
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("login", this.loginForm);

        // console.log(res);

        if (res.meta.status !== 200) {
          return this.$message.error("登录失败");
        }

        // 登录成功之后
        this.$message.success("登陆成功");

        // 1.将登录成功之后的token 保存到客户端的 sessionStorage中
        // 1.1项目中出了登录之外的其他API接口 必须在登录之后才能访问
        // 1.2token只应在网站打开期间生效 所以token保存在sessionStorage中
        // console.log(res)
        window.localStorage.setItem("token", res.data.token);
        this.isShow = !this.isShow;
        console.log(this.isShow);
        // 2.通过编程式导航跳转到后台主页 路由地址是/home
        setTimeout(() => {
          this.$router.push("/profile");
          this.isShow = true;
        }, 1000);

        // this.$bus.$emit("login");

        this.isShow = false;
      });
      // */
    },
    // 重置按钮
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
      this.$emit("resetLoginForm");
    },
  },
};
</script>

<style scoped>
.bounce-enter-active {
  animation: bounce-in 0.5s;
}
.bounce-leave-active {
  animation: bounce-in 0.5s reverse;
}
@keyframes bounce-in {
  0% {
    /* transform: scale(0); */
    opacity: 0;
    width: 0;
    height: 0;
  }
  50% {
    /* transform: scale(1.5); */
  }
  100% {
    /* transform: scale(1); */
    width: 70%;
    height: 30%;
    opacity: 1;
  }
}

.box-card {
  width: 70%;
  height: 30%;
  position: absolute;
  border-radius: 10px;
  /* 居中 */
  left: 50%;
  top: 50%;
  transform: translate(-48%, -50%);
  opacity: 0.95;
  /* background: url("../../../assets/img/regisetr/login.png"); */
  background: url("../../../assets/img/regisetr/login2.jpg");
  background-size: cover;
  box-shadow: 4px 6px 10px #95a5a6;
}

.register-item {
  position: relative;
  margin: auto;
  padding: 0;
  box-sizing: border-box;
  height: 100vh;
  font-size: 16px;
  font-weight: bold;
  text-align: center;
  line-height: 38px;
}

.form {
  width: 100%;
  height: 100%;
}

.username,
.password {
  width: 106%;
  display: flex;
  margin-top: 10px;
  display: block;
}

span {
  width: 30%;
}

.username1,
.password1 {
  width: 80%;
}

.password1 {
  margin-left: 14px;
}

.btns {
  height: 20%;
  width: 100%;
}

.el-form-item__error {
  left: 21%;
}
</style>
